package com.spring.demo;

import com.alibaba.fastjson.JSON;
import com.spring.demo.entity.Order;
import com.spring.demo.entity.UserInfo;
import com.spring.demo.rabbit.ExchangeConst;
import com.spring.demo.rabbit.QueueConst;
import com.spring.demo.rabbit.RoutingKeyConst;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;

/***
 * @title RabbitSend
 * @description
 * @author baiji
 * @version 1.0.0
 * @create 2023/9/4 9:52
 **/
@Slf4j
@SpringBootTest
public class RabbitSend {

    @Resource
    private RabbitTemplate rabbitTemplate;

    @Test
    public void sendSimpleQueue() {
        UserInfo userInfo = new UserInfo();
        userInfo.setUsername("张三");
        userInfo.setPassword("1231892qqeqw");
        userInfo.setEnabled("true");
        rabbitTemplate.convertAndSend(QueueConst.SIMPLE_QUEUE, JSON.toJSONString(userInfo));
    }

    @Test
    public void sendDirectQueue() {
        UserInfo userInfo = new UserInfo();
        userInfo.setUsername("张三");
        userInfo.setPassword("1231892qqeqw");
        userInfo.setEnabled("true");
        rabbitTemplate.convertAndSend(ExchangeConst.DIRECT_EXCHANGE, RoutingKeyConst.DIRECT_ROUTING_KEY_02, JSON.toJSONString(userInfo));
    }

    @Test
    public void sendFanoutQueue() {
        UserInfo userInfo = new UserInfo();
        userInfo.setUsername("张三");
        userInfo.setPassword("1231892qqeqw");
        userInfo.setEnabled("true");
        rabbitTemplate.convertAndSend(ExchangeConst.FANOUT_EXCHANGE, "", JSON.toJSONString(userInfo));
    }

    @Test
    public void sendTopicQueue() {
        UserInfo userInfo = new UserInfo();
        userInfo.setUsername("张三");
        userInfo.setPassword("1231892qqeqw");
        userInfo.setEnabled("true");
        rabbitTemplate.convertAndSend(ExchangeConst.TOPIC_EXCHANGE, "com.woo.hua.ddddd.dasdas", JSON.toJSONString(userInfo));
    }

    @Test
    public void sendDeadLetterMsg() {
        for (int i = 1; i <= 3; i++) {
            Order order = new Order();
            order.setId("2022112600000" + i);
            order.setType(i + "");
            order.setStatus(i + "");
            rabbitTemplate.convertAndSend(ExchangeConst.BUSINESS_EXCHANGE, RoutingKeyConst.BUSINESS_ROUTING_KEY,
                    JSON.toJSONString(order));
        }
    }

    @Test
    public void sendRelayLetterMsg() {
        for (int i = 1; i <= 3; i++) {
            Integer delaySeconds = i;
            String msg = String.format("这是第 %d 条消息", i);
            log.info("开始发送业务消息：{}， 延时时间：{} 秒，发送时间：{}", msg, delaySeconds,
                    LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));

            rabbitTemplate.convertAndSend(ExchangeConst.DELAY_EXCHANGE, RoutingKeyConst.DELAY_ROUTING_KEY,
                    msg, message -> {
                        // 指定延迟时长
                        message.getMessageProperties().setDelay(delaySeconds * 1000);
                        return message;
                    });
        }
    }

}
